using KinonekoSoftware.CodariaDB.Documents.Theories.Chikara;

namespace KinonekoSoftware.Studio.Pages.Theories
{
    public sealed class NewChikaraViewModel : ObjectDialog<Chikara>
    {
        //-------------------------------------------------
        //
        //          Helper Methods
        //
        //-------------------------------------------------

        public static Task<Result<Chikara>> Create() => Dialog<Chikara, NewChikaraViewModel>();

        public static Task<Result<Chikara>> Edit(Chikara chikara)
        {
            var context = new DialogContext();
            context.Set(ContextBase.Key_Value, chikara);
            return Dialog<Chikara, NewChikaraViewModel>(context);
        }

        //-------------------------------------------------
        //
        //          Fields
        //
        //-------------------------------------------------

        private int     _maximum;
        private int     _minimum;
        private string  _name;
        private string  _intro;
        private Chikara _target;


        //-------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------

        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(DialogRule.Create<NewChikaraViewModel>(TextSR.Data_Name, x => !string.IsNullOrEmpty(x.Name)));
            rules.Add(DialogRule.Create<NewChikaraViewModel>("最小值不能大于最大值", x => x.Maximum > x.Minimum));
        }

        protected override bool OnReceive(DialogContext context)
        {
            if (context.Has(ContextBase.Key_Value))
            {
                _target = context.GetObject<Chikara>(ContextBase.Key_Value);
                Maximum = _target.Maximum;
                Minimum = _target.Minimum;
                Name    = _target.Name;
                Intro   = _target.Intro;
                return true;
            }

            return false;
        }

        protected override Chikara OnFinish(bool editing)
        {
            if (editing && _target is not null)
            {
                _target.Maximum = Maximum;
                _target.Minimum = Minimum;
                _target.Name    = Name;
                _target.Intro   = Intro;
                return _target;
            }

            return new Chikara
            {
                Id      = ID.Get(),
                Maximum = Maximum,
                Minimum = Minimum,
                Name    = Name,
                Intro   = Intro,
            };
        }

        //-------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------


        /// <summary>
        /// 获取或设置 <see cref="Intro"/> 属性。
        /// </summary>
        public string Intro
        {
            get => _intro;
            set => TryFinishAndSetValue(ref _intro, value);
        }

        /// <summary>
        /// 获取或设置 <see cref="Name"/> 属性。
        /// </summary>
        public string Name
        {
            get => _name;
            set => TryFinishAndSetValue(ref _name, value);
        }

        /// <summary>
        /// 获取或设置 <see cref="Minimum"/> 属性。
        /// </summary>
        public int Minimum
        {
            get => _minimum;
            set
            {
                if (value >= Maximum)
                {
                    return;
                }

                TryFinishAndSetValue(ref _minimum, value);
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="Maximum"/> 属性。
        /// </summary>
        public int Maximum
        {
            get => _maximum;
            set
            {
                if (value <= Minimum)
                {
                    return;
                }
                TryFinishAndSetValue(ref _maximum, value);
            }
        }
    }
}